#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：turbo 
@File    ：init_sensitive_fields.py
@Author  ：赵加响
@Date    ：2024/6/18 14:16 
@Desc    ：使用init_sensitive_fields命令初始化敏感字段控制相关数据
"""
from django.core.management import BaseCommand

from apps.rbac.models import SensitiveField, Operate, Menu, MenuOperate


class Command(BaseCommand):
    help = 'Initialize User Menu Sensitive Field Data'

    def handle(self, *args, **options):
        self.stdout.write(self.style.SUCCESS('Initialize User Menu Sensitive Field Data.'))
        SensitiveField.objects.get_or_create(code='PRICE',
                                             defaults={'name': '单价', 'name_en': 'Price', 'field_type': 'D'})
        SensitiveField.objects.get_or_create(code='AMOUNT',
                                             defaults={'name': '金额', 'name_en': 'Amount', 'field_type': 'D'})
        SensitiveField.objects.get_or_create(code='CURRENCY',
                                             defaults={'name': '币种', 'name_en': 'Currency', 'field_type': 'C'})

        try:
            authorize = Operate.objects.get(code='AUTHORIZE')
            account = Menu.objects.get(code='RBAC-1010')

            MenuOperate.objects.get_or_create(
                url='/rbac/menu_sensitive_field_authorize/,/rbac/menu_sensitive_field_batch_authorize/,/rbac/get_menu_structure_tree/',
                method='POST',
                menu=account,
                defaults={'operate': authorize})

            self.stdout.write(self.style.SUCCESS('Initialize User Menu Sensitive Field Data Successful.'))
        except (Operate.DoesNotExist, Menu.DoesNotExist):
            self.stdout.write(
                self.style.ERROR('Please execute the initialization command to generate initial data first!'))
